#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//查找子串第一次出现的位置(地址)
char* myStrStr(const char *str, const char *substr)
{
    const char *mystr = str;
    const char *mysub = substr;

    while (*mystr != '\0'){
        if(*mystr != *mysub){
            ++mystr;
            continue;
        }

        //临时指针变量
        const char *temp_mystr = mystr;
        const char *temp_mysub = mysub;
        
        while (*temp_mysub != '\0'){
            if(*temp_mystr != *temp_mysub){
                ++mystr;
                break;
            }

            ++temp_mysub;
            ++temp_mystr;
        }

        //说明匹配成功
        if(*temp_mysub == '\0'){
            return (char *)mystr;
        }
    }

    return NULL;
}

void test()
{
    char *str = "abcdefg";
    char *sub = "de";

    char *pos = myStrStr(str, sub);
    printf("pos=%s\n", pos); //pos=defg
} 

int main(int argc, char const *argv[])
{
    test();
    return 0;
}
